﻿layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var carnos = ["3303", "37", "62", "54", "65"];
    var hacks = [
        "6217855300024010000",
        "6228480395828148670",
        "6228480332964747718",
        "6217002960106071373",
        "320924198109080034",
        "6228480178485429173",
        "6228482599102572475",
        "6228480405923872874",
        "320724199503024835",
        "6236692100000181995",
        "6222021509003806964",
        "6228450266017790000",
        "370685198209064045",
        "6212261606002019779",
        "370685198808274000",
        "6226097812597715",
        "6214857556077446",
        "6217256300009023887",
        "452123198705031010",
        "6212262102002160000",
        "6228483478759099273",
        "6222033100001363228",
        "621226160201908746",
        "6212263100017546800",
        "500228199504101814",
        "628480478594570879",
        "6210983330008116858",
        "6236683030004158437",
        "6217852600017333964",
        "6230521700001524679",
        "6212262201030704409",
        "430321198911187959",
        "332528197812061000",
        "450103196412311020",
        "511026197009282320",
        "612323197908190923",
        "231084197803044254",
        "362202196306152010",
        "341222198908153272",
        "341222198908153272",
        "65422519740504092X",
        "130403197602191222",
        "33032199212030810",
        "421023198311022419",
        "370703198205170327",
        "21080219810706201X",
        "21080219810706201X",
        "130403197602191222",
        "130403197602191222",
        "350583198609048931",
        "622628199401101759",
        "421023198311022419",
        "430422198311153597",
        "130403197602191222",
        "532422197209130023",
        "500230198704251582",
        "330203196910301544",
        "440203198003276722",
        "140104196911252217",
        "330302199411216524",
        "512227197007074315",
        "360121199308128812",
        "500221198601024153",
        "420281199309132430",
        "371324198407105655",
        "370783198310234777",
        "370123199405106239",
        "37052219720805187x",
        "370403198205064528",
        "371521198901163422",
        "372527198402051257",
        "370202194906200427",
        "430321198812096613",
        "429004198301292915",
        "362202196306152010",
        "350624199605163537",
        "211223198102014043",
        "110106196811093041",
        "642123196803030093",
        "360111196203171816",
        "37083019810828029",
        "37070319650922032X",
        "612124197007070057",
        "321283196601060223",
        "330325198107260512",
        "420281199011182443",
        "360426198704164032",
        "131082198209190978",
        "410181198605217533"
    ];

    //注册按钮
    form.on("submit(agentRegister)", function (data) {
        var $this = this;
        $($this).text("注册中...").attr("disabled", "disabled").addClass("layui-disabled");
        var data = {
            UserName: $(".UserName").val(),
            Password: $(".Password").val(),
            RealName: $(".RealName").val(),
            Mobile: $(".Mobile").val(),
            CardId: $(".CardId").val(),
            BankType: $(".BankType").val(),
            BankCard: $(".BankCard").val(),
            InvitationCode: $(".InvitationCode").val(),
            AuditImages: $(".UploadImages").val().split(',')
        };

        //获取防伪标记
        $.ajax({
            type: 'POST',
            url: '/Account/AgentRegister/',
            data: data,
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            //traditional: true,// 防止深度序列化
            success: function (res) { // res为相应体,function为回调函数
                if (res.ResultCode === 200) {
                    var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                        //layer.closeAll("iframe");
                        ////刷新父页面
                        //parent.location.reload();
                        //top.layer.close(alertIndex);
                        window.location.href = "/";
                    });
                } else {
                    $($this).text("注册").prop("disabled", false).removeClass("layui-disabled");
                    layer.alert(res.ResultMsg, { icon: 5 });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $($this).text("注册").prop("disabled", false).removeClass("layui-disabled");
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
        return false;
    });

    // [layui form valid](https://www.layui.com/doc/modules/form.html)
    form.verify({
        username: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '用户名不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '用户名首尾不能出现下划线\'_\'';
            }
            if (/^\d+\d+\d$/.test(value)) {
                return '用户名不能全为数字';
            }
            if (value.indexOf('QS') == 0 || value.indexOf('qs') == 0 || value.indexOf('Qs') == 0 || value.indexOf('qS') == 0) {
                return '用户名不能以QS开头';
            }
            if (value.length > 10) {
                return '用户名太长，请控件在10个字符';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        , pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]
        , confimpass: function (value, item) {
            var password = $("#Password").val();
            if (value != password) {
                return '两次录入的密码不一样';
            }
        }
        , checkcardno: function (value, item) {
            // 温州3303 山东37 甘肃62 西藏54 新 65
            if (value) {
                var result = '';
                for (var i = 0; i < carnos.length; i++) {
                    var item = carnos[i];
                    if (value.indexOf(item) == 0) {
                        result = '此地区（温州，山东，甘肃，西藏，新疆）用户身份证，不能注册！'
                        break;
                    }
                }
                if (result) {
                    return result;
                }
                for (var i = 0; i < hacks.length; i++) {
                    if (value == hacks[i]) {
                        result = '您的身份证已进入黑名单，不能注册！'
                        break;
                    }
                }
                if (result) {
                    return result;
                }
            }
        }
        , checkedimages: function (value, item) {
            if (!value) {
                return '请上传图片资料';
            }
        }
        , checkedread: function (value, item) {
            if (!$("#IsRead").is(":checked")) {
                return "请选择 相关阅读信息";
            }
        }
    });    

    $(".imagesPanel").on('click', '.updateAuditImage', function () {
        var id = $(this).attr("value");
        var newImages = [];
        var images = $("#UploadImages").val().split(',');
        var currentImage = $(".imagesPanel").find("span[value=" + id + "] img").attr("src");
        for (var i = 0; i < images.length; i++) {
            var image = images[i];
            if (currentImage.indexOf(image) == -1) {
                newImages.push(image);
            }
        }
        $("#UploadImages").val(newImages);
        $(".imagesPanel").find("span[value=" + id + "]").remove();
    });

    // 上传图片到七牛云
    $("#uploadImagesBtn").click(function () {
        $("#userfile").click();
    });

    //表单输入效果
    $(".loginBody .input-item").click(function (e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
